//后面改装成 被 包裹
//fileInstantiation.fileClassCode
//fileInstantiation.classCateCode
// $$scope.file = {};
//fileInstantiationDisable.fileClassCodeDis
//fileInstantiationDisable.classCateCodeDis
function extracted($CertusFormAjax, $$scope, fileInstantiation, fileInstantiationDisable) {

    $CertusFormAjax.ajax("listFileClassByCateCode", {fileClassCateCode: $$scope.file.classCategoriesCode}, function (res) {

        $$scope.file.fileClassCodeSelect = res.data;
        $$scope.file.fileClassCode = "xxxx";
        if (fileInstantiation && fileInstantiation.fileClassCode && fileInstantiation.fileClassCode != null) {
            $$scope.file.fileClassCode = fileInstantiation.fileClassCode;
            if (fileInstantiationDisable.fileClassCodeDis) {
                $$scope.fileClassCodeDis = true;
            }
        }
    })
}
function initFileUpload($$scope, fileInstantiation, fileInstantiationDisable, $CertusFormAjax) {
    $$scope.fileview = {};
    $$scope.fileview.semiopen = false;
    $$scope.fileview.disclosure = false;
    $$scope.file = {}
    //初始化类目
    var fileSuperClass = "PJ";
    if(fileInstantiation && fileInstantiation.fileSuperClass){
        fileSuperClass = fileInstantiation.fileSuperClass;
    }

    $CertusFormAjax.ajax("listFileCategory",{"fileCategoryType":fileSuperClass},function(res){
        $$scope.file.classCategoriesCodeSelect = res.data;
        $$scope.file.classCategoriesCode = "xxxx";

        if (fileInstantiation && fileInstantiation.classCateCode && fileInstantiation.classCateCode != "") {
            $$scope.file.classCategoriesCode = fileInstantiation.classCateCode;
            if(fileInstantiationDisable){
                if(fileInstantiationDisable.classCateCodeDis){
                    $$scope.classCateCodeDis = true;
                }
            }
            //extracted($CertusFormAjax, $$scope, fileInstantiation, fileInstantiationDisable);

            $CertusFormAjax.ajax("listFileClassByCateCode", {fileClassCateCode: $$scope.file.classCategoriesCode}, function (res) {

                $$scope.file.fileClassCodeSelect = res.data;
                $$scope.file.fileClassCode = "xxxx";
                if (fileInstantiation && fileInstantiation.fileClassCode && fileInstantiation.fileClassCode != null) {
                    $$scope.file.fileClassCode = fileInstantiation.fileClassCode;
                    if (fileInstantiationDisable.fileClassCodeDis) {
                        $$scope.fileClassCodeDis = true;
                    }
                }
            })
        }
    })

    //初始化类别

    //初始化保密 公开程度
    $$scope.file.classfictionselect = [
        {name: '内部保密', value: "secret"},
        {name: '外部保密', value: "semiopen"},//
        {name: '完全公开', value: "disclosure"}

    ];
    $$scope.file.classfictionCode = "disclosure";



    //类目的 onchange 事件
    $$scope.categoryChange = function () {
        extracted($CertusFormAjax, $$scope, fileInstantiation, fileInstantiationDisable);
    }

    function isSamePinyin(element){
        if(nowPinying){
            nowPinying = nowPinying.toUpperCase();
        }
        var pinyin = "";
        var userName2 = "";
        var userName = "";
        if(element.pinyin){
            pinyin = element.pinyin.toUpperCase();
        }
        if(element.userName2){
            userName2 =  element.userName2.toUpperCase();
        }
        if(element.userName){
            userName = element.userName.toUpperCase();
        }

        //部门名称 联动 查询
        return pinyin.indexOf(nowPinying) > -1 || userName2.indexOf(nowPinying) > -1 || userName.indexOf(nowPinying) > -1;
    }
    var nowPinyin;
    $$scope.$watch('file.fileManager1',function(principalName,oldName,b){

        if (principalName != "") {
            ;
            if(principalName && principalName.indexOf(oldName) > -1 && $$scope.userSelect1 && $$scope.userSelect1.length > 0){
                nowPinying = principalName;
                b.userSelect1 = b.userSelect1.filter(isSamePinyin);
            }else{
                $CertusFormAjax.ajax("relatedUser", {name: principalName}, function (res) {
                    if (res.data && res.data.length > 0) {
                        $$scope.userSelect1 = res.data;
                    }
                });
            }

            if ($$scope.userSelect1 && $$scope.userSelect1.length == 1) {
                var userSelect2 = $$scope.userSelect1[0];
                $$scope.file.fileManager1 = userSelect2.userName;
                $$scope.file.fileManager = userSelect2.userId;
            }

        }
    })
    $$scope.$watch('file.uploaderLeader1',function(principalName,oldName,b){

        if (principalName != "") {
            if(principalName && principalName.indexOf(oldName) > -1 && $$scope.userSelect2 && $$scope.userSelect2.length > 0){
                nowPinying = principalName;
                b.userSelect2 = b.userSelect2.filter(isSamePinyin);
            }else{
                $CertusFormAjax.ajax("relatedUser", {name: principalName}, function (res) {
                    if (res.data && res.data.length > 0) {
                        $$scope.userSelect2 = res.data;
                    }
                });
            }

            if ($$scope.userSelect2 && $$scope.userSelect2.length == 1) {
                var userSelect2 = $$scope.userSelect2[0];
                $$scope.file.uploaderLeader1 = userSelect2.userName;
                $$scope.file.uploaderLeader = userSelect2.userId;
            }
        }
    })

    // $$scope.relatedUser2 = function(principalName){
    //
    //     var flag = false;
    //     if ($$scope.userSelect2 && principalName && principalName != "" && $$scope.userSelect2.length > 0) {
    //         for (var i in $$scope.userSelect2) {
    //             var userSelect2 = $$scope.userSelect2[i];
    //             var s = userSelect2.userName + "(" + userSelect2.userId + ")";
    //             if (s == principalName) {
    //                 $$scope.file.uploaderLeader = userSelect2.userId;
    //                 // $$scope.userSelect2 = {};
    //                 flag = true;
    //             }
    //         }
    //     }
    //
    //     if (principalName != "") {
    //         $CertusFormAjax.ajax("relatedUser", {name: principalName}, function (res) {
    //             if (res.data && res.data.length > 0) {
    //                 $$scope.userSelect2 = res.data;
    //                 if ($$scope.userSelect2.length == 1) {
    //                     var userSelect2 = $$scope.userSelect2[0];
    //                     $$scope.file.uploaderLeader1 = userSelect2.userName + "(" + userSelect2.userId + ")";
    //                     $$scope.file.uploaderLeader = userSelect2.userId;
    //                 }
    //             }
    //         });
    //     }
    // }

    //定义 class选择 变化 执行 函数
    $$scope.classChange = function () {
        if ($$scope.file.classFictionCode == "secret") {
            $$scope.fileview.semiopen = true;
            $$scope.fileview.disclosure = true;
        } else if ($$scope.file.classFictionCode == "semiopen") {
            $$scope.fileview.semiopen = true;
            $$scope.fileview.disclosure = false;
        } else if ($$scope.file.classFictionCode == "disclosure") {
            $$scope.fileview.semiopen = true;
            $$scope.fileview.disclosure = false;
        } else {
            $$scope.fileview.semiopen = false;
            $$scope.fileview.disclosure = false;
        }
    }
}


//文件 公共 提交 方法
function fileUpload($$scope, hasFileFuntionCallback, noFileFunctionCallback, CertusFormAjax) {
    // 开启
    // var index = layer.load(1);
    //关闭
    // layer.close(index);

    if(certusValidateNotnullPolyfill()){
        if (CertusFormAjax == null) {
            CertusFormAjax = $CertusFormAjax;
        }
        var form = new FormData();
        var file = document.getElementById("fileUploadId").files[0];
        form.append('file', file);
        if (file && file.size > 0) {
            if(file.size > 50*1024*1024){
                layer.msg("文件暂时 不能大于50M");
                return;
            }
            var type = file.type;
            ;
            var lastIndexOf = file.name.lastIndexOf(".");
            if(lastIndexOf){
                var substring = file.name.substring(lastIndexOf,file.name.length).toLowerCase();
                form.append("fileName", $$scope.file.fileName+substring);
                if(substring.indexOf("doc")>-1 ||
                    substring.indexOf("xls")>-1||
                    substring.indexOf("pdf")>-1 ||
                    substring.indexOf("ppt")>-1 ||
                    substring.indexOf("rar")>-1 ||
                    substring.indexOf("zip")>-1 ||
                    substring.indexOf("txt")>-1){
                }else{
                    layer.msg("文件暂时 只支持 txt excel word ppt pdf 等文件格式");
                    return;
                }
            }

            form.append("fileCategories",$$scope.file.classCategoriesCode);
            form.append("fileClass",$$scope.file.fileClassCode);
            if($$scope.file.fileDesc){
                form.append("fileDescriptor",$$scope.file.fileDesc);
            }

            form.append("fileManager",$$scope.file.fileManager);
            if(($$scope.fileview.semiopen && $$scope.file.fileManager1) && !$$scope.file.fileManager){
                layer.msg("请填写正确文件管理员");
                return;
            }
            form.append("uploaderLeader",$$scope.file.uploaderLeader);
            if(($$scope.fileview.disclosure && $$scope.file.uploaderLeader1) && !$$scope.file.uploaderLeader){
                layer.msg("请填写正确下载审批人");
                return;
            }
            form.append("classFictionCode", $$scope.file.classFictionCode);
            // form.append("fileName", $$scope.file.fileName);
            // form.append("range", $$scope.file.range);
            // form.append("permission", $$scope.file.permission);
            form.append("validity", $$scope.file.validity);
            CertusFormAjax.upload("processUploadFile", form, hasFileFuntionCallback);
        } else {
            if(noFileFunctionCallback){

                noFileFunctionCallback();
            }else{
                layer.msg("抱歉，你正在上传空文件");
            }
        }
    }
}

//文件 公共 提交 方法
function fileUpload1($$scope, hasFileFuntionCallback, noFileFunctionCallback, CertusFormAjax) {
    // 开启
    // var index = layer.load(1);
    //关闭
    // layer.close(index);

    if(certusValidateNotnullPolyfill()){
        if (CertusFormAjax == null) {
            CertusFormAjax = $CertusFormAjax;
        }
        var form = new FormData();
        var file = document.getElementById("fileUploadId").files[0];
        form.append('file', file);
        if (file && file.size > 0) {
            if(file.size > 50*1024*1024){
                layer.msg("文件暂时 不能大于50M");
                return;
            }
            var type = file.type;
            ;
            var lastIndexOf = file.name.lastIndexOf(".");
            if(lastIndexOf){
                var substring = file.name.substring(lastIndexOf,file.name.length).toLowerCase();
                form.append("fileName", $$scope.file.fileName+substring);
                if(substring.indexOf("doc")>-1 ||
                    substring.indexOf("xls")>-1||
                    substring.indexOf("pdf")>-1 ||
                    substring.indexOf("ppt")>-1 ||
                    substring.indexOf("rar")>-1 ||
                    substring.indexOf("zip")>-1 ||
                    substring.indexOf("txt")>-1){
                }else{
                    layer.msg("文件暂时 只支持 txt excel word ppt pdf 等文件格式");
                    return;
                }
            }

            form.append("fileCategories",$$scope.file.classCategoriesCode);
            form.append("fileClass",$$scope.file.fileClassCode);
            form.append("fileDescriptor",$$scope.file.fileDesc);

            form.append("fileManager",$$scope.file.fileManager);
            form.append("uploaderLeader",$$scope.file.uploaderLeader);
            form.append("classFictionCode", $$scope.file.classFictionCode);
            // form.append("fileName", $$scope.file.fileName);
            // form.append("range", $$scope.file.range);
            // form.append("permission", $$scope.file.permission);
            form.append("validity", $$scope.file.validity);
            CertusFormAjax.upload("processUploadFile", form, hasFileFuntionCallback);
        } else {
            if(noFileFunctionCallback){

                noFileFunctionCallback();
            }else{
                layer.msg("抱歉，你正在上传空文件");
            }
        }
    }
}

